// pages/task/detail.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    id: 0,
    nowTime: 0,
    detail: {},
    loaddinghidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var id = options.id;
    if (!id) {
      wx.showModal({
        title: '加载失败！',
        success: function(res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    }

    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    // 显示 loading
    that.setData({
      id: id,
      loaddinghidden: false
    });

    that.loadData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var now = new Date().getTime() / 1000;
    this.setData({
      nowTime: now
    })
  },
  loadData: function() {
    var that = this;
    var parameter = {};
    parameter['id'] = that.data.id;

    http.ApiGet(apiUrl.UrlTaskDetail, parameter, function(res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        that.historyAdd();
        that.setData({
          loaddinghidden: true,
          detail: res.data,
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadData();
  },
  historyAdd: function() {
    var that = this;
    var formData = {};
    formData['id'] = that.data.id;
    formData['type'] = "6";
    http.ApiPost(apiUrl.UrlHistoryAdd, formData, function(res) {
      if (res.code == 1) {
        console.log(res.msg);
      } else {
        // wx.showModal({
        //   title: res.msg
        // });
        console.log(res.msg);
      }
    });
  },
  positionTap: function() {
    var latitude = parseFloat(this.data.detail.latitude);
    var longitude = parseFloat(this.data.detail.longitude);
    var position = this.data.detail.position;
    console.log(latitude);
    console.log(longitude);
    console.log(position);
    if (!isNaN(latitude) && !isNaN(longitude)) {
      wx.openLocation({
        latitude: latitude,
        longitude: longitude,
        name: position,
        scale: 18,
        fail(err) {
          console.log(err);
        }
      })
    }
  },
  imageTap: function(e) {
    var imgs = [e.currentTarget.dataset.src];
    // console.log("imageTap:" + imgs);
    wx.previewImage({
      urls: imgs
    })
  },
  documentTap: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../document/detail?id=' + id
    })
  },
  doSubmit: function() {
    var id = this.data.id;
    var detail = this.data.detail;
    wx.navigateTo({
      url: '../document/submit?task_id=' + id + '&topic_id=' + detail.topic_ids + '&task_title=' + detail.title
    })
  },
})